﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPMS.Model;
namespace GPMS.Web.Controllers
{
    /// <summary>
    /// 毕业设计计划控制器
    /// </summary>
    public class PlanController :BaseController
    {

        //public ActionResult PList(Guid id)//当前用户的学生的计划列表
        //{
        //    var list = gpmsDb.ProjectPlans.Where(p=>p.GPSelection.UserID== id);
        //    return View(list);
        //}
        
        //public ActionResult SPList()//学生计划显示
        //{
        //    Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
        //    return View(gpmsDb.ProjectPlans.Where(p=>p.GPSelection.IsConfirm && p.GPSelection.UserID==id));
        //}
        public ActionResult StudentList()
        {
            Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            GPSelection selection = gpmsDb.GPSelections.FirstOrDefault(s => s.IsConfirm && s.UserID == id);
            if (selection ==null)
            {
                return Content("您没有确定的毕业设计！");
            }
            else
            {
               return  RedirectToAction("List", new { id = selection.SelectionID });
            }
        }

        
        //计划列表
        public ActionResult List(Guid id)
        {
             Guid userid = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            var list = gpmsDb.ProjectPlans.Where(g => g.SelectionID == id).OrderBy(g=>g.BeginDate);
            ViewData["gp"] = gpmsDb.GPSelections.FirstOrDefault(g => g.SelectionID == id).GraduationProject;
            ViewData["guider"] = gpmsDb.GPSelections.FirstOrDefault(g => g.SelectionID == id).GraduationProject.Guider;
            ViewData["student"] = gpmsDb.GPSelections.FirstOrDefault(g => g.SelectionID == id).User.RealName;
            ViewData["selection"] = gpmsDb.GPSelections.FirstOrDefault(g => g.SelectionID == id);
            ViewData["finish"] = gpmsDb.GPSelections.FirstOrDefault(g => g.SelectionID == id).User.Status == GPMS.Extension.UserStatusEnum.Answering.ToString();
            ViewData["isGuider"] = gpmsDb.GPSelections.FirstOrDefault(g => g.SelectionID == id).GraduationProject.Guider.UserID == userid;
            return View(list);
        }

        //
        // GET: /Plan/Create

        public ActionResult Create(Guid id)
        {
            ProjectPlan plan = new ProjectPlan() {
                SelectionID = id,
                BeginDate=DateTime.Now,
                FinishDate=DateTime.Now.AddDays(30)
            };
            return View(plan);
        } 

        //
        // POST: /Plan/Create

        [HttpPost]
        public ActionResult Create(ProjectPlan plan,FormCollection collection)//计划创建
        {
            try
            {
                    plan.PlanID = Guid.NewGuid();
                    plan.IsDone=false;
                    plan.RealFinishDate = null;
                    plan.Mark = null;
                    plan.Evaluation = null;
                    gpmsDb.ProjectPlans.AddObject(plan);
                    gpmsDb.SaveChanges();
                    return Success("课题计划发布成功");
            }
            catch(Exception ex)
            {
                return JsonError(ex.Message);
            }
        }
        
        //
        // GET: /Plan/Edit/5

        public ActionResult Edit(Guid id)
        {
            ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
            return View(plan);
        }

        //
        // POST: /Plan/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, FormCollection collection)//计划修改
        {
            try
            {
                ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
                UpdateModel(plan, collection);
                gpmsDb.SaveChanges();
                return Success("修改成功");
            }
            catch(Exception ex)
            {
                return JsonError("修改失败"+ex);
            }
        }

        //
        // GET: /Plan/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            return Confirm("删除计划?");
        }

        //
        // POST: /Plan/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)//删除计划
        {
                ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
                gpmsDb.ProjectPlans.DeleteObject(plan);
                gpmsDb.SaveChanges();
                return Success("删除成功！！");
        }

        public ActionResult ReadyForAnswer(Guid id)
        {
            if (gpmsDb.GPSelections.FirstOrDefault(g=>g.SelectionID==id).ProjectPlans.Any(p=>!p.IsDone))
            {
                return Error("还有计划未完成，不能进入答辩状态。");
            }
            return Confirm("确定进入可答辩状态?");
        }

        //
        // POST: /Plan/Delete/5

        [HttpPost]
        public ActionResult ReadyForAnswer(Guid id, FormCollection collection)//删除计划
        {
            try
            {
                UserInfo user= gpmsDb.GPSelections.FirstOrDefault(gps=>gps.SelectionID==id).User;
                user.Status = GPMS.Extension.UserStatusEnum.Answering.ToString();
                gpmsDb.SaveChanges();
                return Success("进入可答辩状态成功！！");
            }
            catch (Exception ex)
            {
                return JsonError("进入可答辩状态失败" + ex);
            }
        }

        

        public ActionResult Rating(Guid id)
        {
            ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
            return View(plan);
        }

        [HttpPost]
        public ActionResult Rating(Guid id, FormCollection collection)//计划完成
        {
            try
            {
                ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
                UpdateModel(plan, collection);
                plan.IsDone = true;
                plan.RealFinishDate = DateTime.Now;
                gpmsDb.SaveChanges();
                return Success("评分成功！！");
            }
            catch (Exception ex)
            {
                return JsonError("评分失败" + ex);
            }
            
        }

        public ActionResult SelectFile(Guid id)
        {
            ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
             Guid userid = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            var files= GPMS.BLL.FileHelper.GetUserFileList(userid, gpmsDb);
            files.Insert(0, new File() { FileID = Guid.Empty, Name = "---无---" });
            ViewData["files"] = files;
            return View(plan);
        }

        [HttpPost]
        public ActionResult SelectFile(Guid id, FormCollection collection)
        {
            try
            {
                ProjectPlan plan = gpmsDb.ProjectPlans.FirstOrDefault(p => p.PlanID == id);
                UpdateModel(plan, collection);
                if (plan.FileID == Guid.Empty)
                {
                    plan.FileID = null;
                }
                gpmsDb.SaveChanges();
                return Success("选择文件成功！！");
            }
            catch (Exception ex)
            {
                return JsonError("选择文件失败" + ex);
            }

        }
    }
}
